#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set ts=4 sw=4 noet:
#
# $Id$

"""
PatternManager.py
"""

__version__ = "$Revision$"
# $URL$

import gtk
from gettext import gettext as _

ui_str = """<ui>
  <menubar name="MenuBar">
    <menu name="ToolsMenu" action="Tools">
      <placeholder name="ToolsOps_6">
        <menuitem name="Manage Patterns" action="LaunchPatternManager"/>
      </placeholder>
    </menu>
  </menubar>
</ui>
"""

class PatternManager(object):
	def __init__(self, plugin, window, gladeObject):
		"""
		Takes the plugin, window and gladeObject.
		"""
		self._window = window
		self._plugin = plugin
		# Insert menu items
		self._insert_menu()
		self._loadGlade(gladeObject)

	def stop(self):
		# Remove any installed menu items
		self._remove_menu()
		self._window = None
		self._plugin = None
		self._action_group = None
		
	def _insert_menu(self):
		# Get the GtkUIManager
		manager = self._window.get_ui_manager()
		# Create a new action group
		self._action_group = gtk.ActionGroup("PatternManagerActions")
		tipText = "Launch the Pattern Manager."
		self._action_group.add_actions([("LaunchPatternManager", None, _("Manage Patterns..."), None, tipText, self.launchPatternManager)])
		# Insert the action group
		manager.insert_action_group(self._action_group, -1)
		# Merge the UI
		self._ui_id = manager.add_ui_from_string(ui_str)

	def _loadGlade(self, gladeObject):
		"""
		Grabs widgets from the gladeObject.
		"""
		## Pattern Manager.
		self._patternManager = gladeObject.get_widget("PatternManager")
		self._wrapWindow(self._patternManager)
		# Treeview and control buttons.
		self._patternTreeview = gladeObject.get_widget("patternTreeview")
		self._patternTreeview.get_selection().set_mode(gtk.SELECTION_NONE)
		self._batchButton = gladeObject.get_widget("batchButton")
		self._cancelButton = gladeObject.get_widget("cancelButton")
		self._cancelButton.connect("clicked", self._hideButtonClicked, self._patternManager)
		self._deleteButton = gladeObject.get_widget("deleteButton")
		self._useButton = gladeObject.get_widget("useButton")
		# Setup columns.
		self._patternModel = gtk.TreeStore(str, str, str, int, bool, bool, bool, bool)
		self._patternTreeview.set_model(self._patternModel)
		for index, columnName in enumerate(["Name", "Find", "Replace", "Selected", "Ignore case", "Interpret locale", "Multi-line", "Dot-all"]):
			if columnName == "Selected":
				column = gtk.TreeViewColumn(columnName, gtk.CellRendererToggle(), selected=index)
			else:
				column = gtk.TreeViewColumn(columnName, gtk.CellRendererText(), text=index)
			self._patternTreeview.append_column(column)
		# Hide the dialogs for now.
		self._fakePatternData()
		self._patternManager.hide()		

	def update(self):
		# Called whenever the window has been updated (active tab
		# changed, etc.)
		pass

	def _wrapWindow(self, window):
		window.connect("delete-event", self._hideWindow)
		window.connect("destroy-event", self._hideWindow)
		window.set_transient_for(self._window)

	def _hideButtonClicked(self, button, window):
		self._hideWindow(window, None)

	def _hideWindow(self, window, event):
		window.hide()
		return True

	def launchPatternManager(self, action):
		"""
		Launches the pattern manager.
		"""
		self._presentWindow(self._patternManager)

	def _presentWindow(self, window):
		# Show.
		window.show_all()
		if not window.is_focus():
			window.present()
		window.grab_focus()

	def _fakePatternData(self):
		"""
		Add some fake data.
		"""
		formattingIter = self._patternModel.append(None, ("Formatting", "", "", 0, None, None, None, None))
		treeIter = self._patternModel.append(formattingIter, ("Strip spaces from around tabs.", " *\t *", "\t", 0, False, False, False, False))
		treeIter = self._patternModel.append(formattingIter, ("Strip line-initial whitespace.", "^\s*", "", 0, False, False, False, False))
		treeIter = self._patternModel.append(formattingIter, ("Strip line-final whitespace.", "\s*$", "", 0, False, False, False, False))
		userIter = self._patternModel.append(None, ("My Patterns", "", "", 0, None, None, None, None))
		treeIter = self._patternModel.append(userIter, ("Some pattern", "", "", 0, False, False, False, False))
		self._patternTreeview.expand_all()

# End of $URL$
